const Router = require('koa-router')
const koaJwt = require('koa-jwt');
const secretKey = require('../../config/keys').secretKey;
const Address = require('../../models/address');

const router = new Router()

/**
 * @method GET
 * @route /address/getAddressList
 * @param 
 * @description 用于获取收获地址列表 需要token验证
 */
router.get('/address/getAddressList',
  koaJwt({ secret: secretKey }),
  async ctx => {
    const payload = ctx.state.user
    const findRes = await Address.find({ uid: payload._id })
    ctx.status = 200
    ctx.body = findRes
  }
)

/**
 * @method POST
 * @route /address/addAddress
 * @param 
 * @description 用于添加收获地址
 */
router.post(
  '/address/addAddress',
  koaJwt({ secret: secretKey }),
  async ctx=>{
    const body = ctx.request.body
    const newAddress = new Address({
      ...body
    })
    await newAddress.save()
      .then(res=>{
        console.log(res)
        ctx.status=200
        ctx.body={msg:'新增成功'}
      })
      .catch(err=>{
        console.log(err)
        ctx.status=500
        ctx.body=err
      })
  }
)

module.exports = router.routes()